#include "base64enckai.h"
#include <string>


namespace {
static std::string base64enccode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_";
}


extern "C" char* __stdcall base64enckai(const std::string &ch ) {
unsigned int x, n=0, N=ch.size(); 
static std::string str; str="";
do{ 
x=0;
int k=N-n; if(k>3) k=3;
memcpy(&x, &ch[n], k); n+=k;
for( k=0; k<4; k++) { str += base64enccode[x%64]; x/=64; }
} while( n < N ); 
return &str[0]; }




extern "C" char* __stdcall base64deckai( const std::string &ch ) {
static std::string inv; static int flg=0;
unsigned int x,n=0,i;
if(!flg) { inv.resize(128); for(i=0;i<base64enccode.size();i++) inv[base64enccode[i]]=(char)i; }
static std::string str; str="";
do {
x=0; for(i=0; i<4 ; i++) x+=inv[ch[n+i]]<<(6*i);
n+=4;
i=str.size(); str.resize(i+3); memcpy(&str[i],&x,3);
}while(n<ch.size()); return &str[0]; }